package sdk.org.apache.shiro.G2_使用.C07_1Web集成_登录和退出.shiro;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import com.titan.core.utils.io.ConsoleUtils;

import sdk.org.apache.shiro.utils.ShiroUtils;

public class MyShiroRealm extends AuthorizingRealm {
    //角色权限和对应权限添加
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获取登录用户名
        String name= ShiroUtils.getUsername(principalCollection, this);
        ConsoleUtils.method(MyShiroRealm.class, "doGetAuthorizationInfo", "name", name);
        //添加角色和权限
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        return simpleAuthorizationInfo;
    }

    
    //用户认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //获取用户信息
        String username = ShiroUtils.getUsername(token);
        String password = ShiroUtils.getPassword(token);
        ConsoleUtils.method(MyShiroRealm.class, "doGetAuthenticationInfo", "username", username, "password", password);
        
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(username, password, getName());
        return simpleAuthenticationInfo;
    }
}
